package threecard.android;

import threecard.android.card.CardStack;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

/**
 * This implements only the front-end. Most of the calculations will be carried
 * out at the server.
 * 
 * @author X
 * 
 */
public class Game extends MainActivity {
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// Create a linear layout.
		LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
				LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
		LinearLayout rootLayout = new LinearLayout(this);
		rootLayout.setOrientation(1); // vertical
		rootLayout.setLayoutParams(params);

		try {

			/**
			 * TEST : Draw two cards CardStack cs1 = new CardStack(1, 1, 2, 2,
			 * 3, 3); cs1.setFlipable(true); RelativeLayout rl1 =
			 * cs1.drawStack(this); rl1.setPadding(40, 60, 0, 0);
			 * 
			 * CardStack cs2 = new CardStack(2, 2, 1, 1, 3, 3);
			 * cs1.setFlipable(true); RelativeLayout rl2 = cs2.drawStack(this);
			 * rl2.setPadding(100, 60, 0, 0);
			 * 
			 * rootLayout.addView(rl1); rootLayout.addView(rl2);
			 * 
			 * setContentView(rootLayout); } catch (Exception e) {
			 * e.printStackTrace(); }
			 */
			// End TEST.
			// Trying to create 4 card decks using array.
			// This will illustrate how to draw a variable number of card decks.
			CardStack cs[] = new CardStack[4];
			RelativeLayout rl[] = new RelativeLayout[4];
			for (int i = 0; i < 4; i++) {
				cs[i] = new CardStack(i + 1, 1, i + 2, 2, i + 3, 3);
				rl[i] = cs[i].drawStack(this);
				
				Log.w("Compare the deck number " + i + "to null:", "" + (rl[i] == null));
				Log.w("Drawing card deck", "Relative layout: " + rl[i].toString());

				// Set padding. This will specify the position of the deck
				// within the relative layout.
				if (i % 2 == 0) {
					rl[i].setPadding(40, 60, 0, 0);
				} else {
					rl[i].setPadding(120, 60, 0, 0);
				}

				// Add the relative layout to our root layout: the linear
				// layout.
				rootLayout.addView(rl[i]);
			}

			setContentView(rootLayout);
		} catch (Exception e) {
			// TODO: handle exception
			Log.e("Drawing card", "Unhandled exception" + e.getMessage());
		}
	}

	// when user hits back button, application prompts user whether they really
	// want to exit game
	@Override
	public void onBackPressed() {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage("Are you sure you want to exit?")
				.setCancelable(false)
				.setPositiveButton("Yes",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {

								finish();
							}
						})
				.setNegativeButton("No", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int id) {
						dialog.cancel();
					}
				});
		AlertDialog alert = builder.create();
		alert.show();

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		getMenuInflater().inflate(R.menu.gameoptions, menu);
		menu.findItem(R.id.help_menu_item).setIntent(
				new Intent(this, Help.class));
		menu.findItem(R.id.settings_menu_item).setIntent(
				new Intent(this, Settings.class));
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		super.onOptionsItemSelected(item);
		startActivity(item.getIntent());
		return true;
	}
}// end of class Game
